﻿/*
 * The MIT License (MIT)
 * Copyright (c) 2012 @donkeysharp
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace DonkeyCleaner.Core {
    public static class HashUtil {
        /// <summary>
        /// Computes the MD5-Sum from of a specific file
        /// </summary>
        /// <param name="filename">File name</param>
        /// <returns>The MD5-Sum of a specific file</returns>
        public static string ComputeFileMD5Sum(string filename) {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            Stream file = File.Open(filename, FileMode.Open);
            byte[] md5Sum = md5.ComputeHash(file);
            file.Close();
            return BitConverter.ToString(md5Sum).Replace("-", string.Empty);
        }
        /// <summary>
        /// Computes the SHA1-Sum from of a specific file
        /// </summary>
        /// <param name="filename">File name</param>
        /// <returns>The SHA1-Sum of a specific file</returns>
        public static string ComputeFileSHA1Sum(string filename) {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            Stream file = File.Open(filename, FileMode.Open);
            byte[] sha1Sum = sha1.ComputeHash(file);
            file.Close();
            return BitConverter.ToString(sha1Sum).Replace("-", string.Empty);
        }
        /// <summary>
        /// Computes the SHA256-Sum from of a specific file
        /// </summary>
        /// <param name="filename">File name</param>
        /// <returns>The SHA256-Sum of a specific file</returns>
        public static string ComputeFileSHA256Sum(string filename) {
            SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
            Stream file = File.Open(filename, FileMode.Open);
            byte[] sha256Sum = sha256.ComputeHash(file);
            file.Close();
            return BitConverter.ToString(sha256Sum).Replace("-", string.Empty);
        }
        /// <summary>
        /// Computes the SHA512-Sum from of a specific file
        /// </summary>
        /// <param name="filename">File name</param>
        /// <returns>The SHA512-Sum of a specific file</returns>
        public static string ComputeFileSHA512Sum(string filename) {
            SHA512CryptoServiceProvider sha512 = new SHA512CryptoServiceProvider();
            Stream file = File.Open(filename, FileMode.Open);
            byte[] sha512Sum = sha512.ComputeHash(file);
            file.Close();
            return BitConverter.ToString(sha512Sum).Replace("-", string.Empty);
        }
    }
}
